<?php
namespace app\api\controller;
use app\api\controller\CommonController;
use think\Validate;
use app\api\model\ImsMemberModel;
use app\api\model\ImsUserFollowModel;
class PersonalcenterController extends CommonController
{
    /**
     * 个人资料
     * @params string token_key
     *
     */
    public function personaldata()
    {
        $token = input('post.token_key');
        $rule = [
            'token' => 'require'
        ];
        $msg = [
            'token.require' => 4002
        ];
        $data['token'] = $token;
        $validate = new Validate($rule, $msg);
        $checkParam = $validate->check($data);
        if(!$checkParam){
            $errcode = $validate->getError();
            return returnJson($errcode, $this->errTips[$errcode]);
        }

        $verifyToken = $this->verifyToken($token);
        if ( $verifyToken != '1111') {return $verifyToken;}
        $user_id = $this->user_id;

        $user_m = new ImsMemberModel();
        $userQueryResult = $user_m->field('id,staff_name,staff_number,staff_duty,staff_pic,staff_deptname,create_time,motto')->find($user_id);
        $personalFollowUserCount = count($userQueryResult->getfollowuser);
        $getfansCount = count($userQueryResult->getfans);
        $arrayData = $userQueryResult->toArray();
        $arrayData['personalFollowUserCount'] = $personalFollowUserCount;
        $arrayData['getfansCount'] = $getfansCount;
        unset($arrayData['getfans']);
        unset($arrayData['getfollowuser']);
        return returnJson(200, '', $arrayData);

    }
    public function getfollowlist()
    {
        $token = input('post.token_key');
        $rule = [
            'token' => 'require'
        ];
        $msg = [
            'token.require' => 4002
        ];
        $data['token'] = $token;
        $validate = new Validate($rule, $msg);
        $checkParam = $validate->check($data);
        if(!$checkParam){
            $errcode = $validate->getError();
            return returnJson($errcode, $this->errTips[$errcode]);
        }
        $verifyToken = $this->verifyToken($token);
        if ( $verifyToken != '1111') {return $verifyToken;}
        $user_id = $this->user_id;

        $userFollowModel = new ImsUserFollowModel();
        $whereMap['user_id'] = $user_id;
        $whereMap['status'] = 1;
        $selectResult = $userFollowModel->where($whereMap)->select();
        foreach($selectResult as $sv){
            $getuserinfo = $sv->getuserinfo;
            $datas = $sv->toArray();

            $datas['user_name'] = $getuserinfo['staff_name'];
            $datas['user_duty'] = $getuserinfo['staff_duty'];
            $datas['user_pic'] = $getuserinfo['staff_pic'];
            unset($datas['getuserinfo']);
            $fdata[] = $datas;
        }
        return returnJson(200, 'success', $fdata);
    }
}